package com.lc.zpyh.sqlite;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.lc.zpyh.bean.StockEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhanghuanyu on 2019/3/1.
 */

public class RecordsDao {
    RecordSQLiteOpenHelper recordHelper;

    SQLiteDatabase recordsDb;

    public RecordsDao(Context context) {
        recordHelper = new RecordSQLiteOpenHelper(context);
    }
    //添加搜索记录
    public void add(String name, String code) {
        Log.e("##", "onItemClick: "+ name+code);
        String sql="insert into stockentity (name,code) values (?,?)";
        String bindArgs[] = new String[] { name, code };
        recordsDb = recordHelper.getWritableDatabase();
        recordsDb.execSQL(sql,
                bindArgs);
        recordsDb.close();
    }


    //判断是否含有该搜索记录
    public boolean isHasRecord(String name) {
        boolean isHasRecord = false;
        recordsDb = recordHelper.getReadableDatabase();
        String sql="select * from stockentity where name=?";
        Cursor cursor = recordsDb.rawQuery(sql,
                new String[] { name });

        while (cursor.moveToNext()) {
            if (name.equals(cursor.getString(cursor.getColumnIndexOrThrow("name")))) {
                isHasRecord = true;
            }
        }
        //关闭数据库
        recordsDb.close();
        cursor.close();
        return isHasRecord;
    }
    public List<StockEntity> findAll() {
        List<StockEntity> persons = new ArrayList<StockEntity>();
        recordsDb = recordHelper.getReadableDatabase();
        String sql="select * from stockentity";
        Cursor cursor = recordsDb.rawQuery(sql, null);
        while(cursor.moveToNext()){
            StockEntity   person = new StockEntity();
//            person.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
            person.setCode(cursor.getString(cursor.getColumnIndex("code")));
            person.setName(cursor.getString(cursor.getColumnIndex("name")));


//            int id = cursor.getInt(cursor.getColumnIndex("id"));
//            String name = cursor.getString(cursor.getColumnIndex("name"));
//            String code = cursor.getString(cursor.getColumnIndex("code"));
//            StockEntity person = new StockEntity(id, name, code);
            persons.add(person);
        }
        cursor.close();
        recordsDb.close();

        return persons;
    }
    //获取全部搜索记录
    public List<String> getRecordsList() {
        List<String> recordsList = new ArrayList<>();
        recordsDb = recordHelper.getReadableDatabase();
        Cursor cursor = recordsDb.query("records", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
            recordsList.add(name);
        }
        //关闭数据库
        recordsDb.close();
        cursor.close();
        return recordsList;
    }

    //模糊查询
    public List<String> querySimlarRecord(String record,String code){
        String queryStr = "select * from records where name like '%" + record + "%' order by name ";
        List<String> similarRecords = new ArrayList<>();
        Cursor cursor= recordHelper.getReadableDatabase().rawQuery(queryStr,null);

        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
            similarRecords.add(name);
        }

        cursor.close();
        return similarRecords;
    }

    //清空搜索记录
    public void deleteAllRecords() {
        String sql="delete from stockentity";
        recordsDb = recordHelper.getWritableDatabase();
        recordsDb.execSQL(sql);

        recordsDb.close();
    }

}
